# encoding: utf-8

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io

def _escape(text):
    text = text.replace('\\', r'\textbackslash')
    text = text.replace(r'{', r'\{')
    text = text.replace(r'}', r'\}')
    text = text.replace(r'\textbackslash', r'\textbackslash{}')

    text = text.replace(r'<', r'\textless{}')
    text = text.replace(r'>', r'\textgreater{}')
    text = text.replace(r'|', r'\textbar{}')
    text = text.replace(r'"', r'\textquotedbl{}')
    text = text.replace(r'~', r'\textasciitilde{}')
    text = text.replace(r'#', r'\#')
    text = text.replace(r'$', r'\$')
    text = text.replace(r'%', r'\%')
    text = text.replace(r'^', r'\^{}')
    text = text.replace(r'&', r'\&')
    text = text.replace(r'_', r'\_')
    text = text.replace(r'-', r'-{}')
    return text


def _write(output_stream, dom):
    #TODO: Make the output prettier.
    if dom.type == 'root':
        for node in dom.children:
            _write(output_stream, node)
    elif dom.type == 'paragraph':
        output_stream.write('\n\n')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('\n\n')
    elif dom.type == 'block quote':
        output_stream.write('\\begin{quote}\n')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('\\end{quote}\n')
    elif dom.type == 'list item':
        output_stream.write('\item ')
        for node in dom.children:
            _write(output_stream, node)
    elif dom.type == 'unordered list':
        output_stream.write('\\begin{itemize}\n')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('\\end{itemize}\n')
    elif dom.type == 'ordered list':
        output_stream.write('\\begin{enumerate}\n')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('\\end{enumerate}\n')
    elif dom.type == 'header':
        #TODO: Allow level 1 to be used as title?
        if header.level == 1:
            output_stream.write('\part{')
        elif header.level == 2:
            output_stream.write('\chapter{')
        elif header.level == 3:
            output_stream.write('\section{')
        elif header.level == 4:
            output_stream.write('\subsection{')
        elif header.level == 5:
            output_stream.write('\subsubsection{')
        elif header.level == 6:
            output_stream.write('\paragraph{')
        else:
            #TODO: Warning or error.
            pass
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('}')
    elif dom.type == 'nbsp':
        output_stream.write('~')
    elif dom.type == 'ellipsis':
        output_stream.write('\ldots{}')
    elif dom.type == 'en dash':
        output_stream.write('--')
    elif dom.type == 'em dash':
        output_stream.write('---')
    elif dom.type == 'quote':
        output_stream.write('``')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('\'\'')
    elif dom.type == 'strong':
        output_stream.write('\\textbf{')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('}')
    elif dom.type == 'emphasized':
        output_stream.write('\\emph{')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('}')
    elif dom.type == 'code':
        output_stream.write('\\texttt{')
        for node in dom.children:
            _write(output_stream, node)
        output_stream.write('}')
    elif dom.type == 'text':
        output_stream.write(_escape(dom.text))


def convert(dom):
    with io.StringIO() as output_stream:
        _write(output_stream, dom)
        return output_stream.getvalue()

